package com.sm.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sm.dao.MyVipMapper;
import com.sm.dao.VipDictionaryMapper;
import com.sm.model.HappyRequest;
import com.sm.model.MyVip;
import com.sm.model.User;
import com.sm.model.VipDictionary;
import com.sm.util.CommonUtil;
import com.sm.util.PageBean;
import com.sm.util.PageResult;
import com.sm.vo.UserTeamApplyVO;
import com.sm.vo.UserTeamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sm.dao.UserMapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private VipDictionaryMapper vipDictionaryMapper;
	@Autowired
	private MyVipMapper myVipMapper;

	public User selectUserById(Integer userId) {

		// 根据用户id查询用户。
		User user = userMapper.selectByPrimaryKey(userId);
		return user;
	}

	//根据用户id获取用户的头像和昵称（幸福问答模块使用）
	public User selectAvatarAndNickName(Integer userId){
		return userMapper.selectAvatarAndNickName(userId);
	}

	public User selectByPhone(String phone) {
		return userMapper.selectByPhone(phone);
	}

	public User selectByToken(String token) {
		return userMapper.selectByToken(token);
	}

	public User selectByRecommendCode(String recommendCode) {
		return userMapper.selectByRecommendCode(recommendCode);
	}

	public int insertSelective(User record) {
		record.setNickName("圆梦"+ CommonUtil.randomCode());
		record.setCreateTime(new Date());
		record.setUpdateTime(new Date());
		userMapper.insertSelective(record);
		//注册送家庭教育会员到2020年12月31号
//		System.out.println("有人注册ID=" + record.getUserId());
//		//如果是2020年注册的就送VIP
//		if (DateUtil.thisYear()==2020) {
//			VipDictionary vipDictionary = vipDictionaryMapper.selectByPrimaryKey(0L);
//			MyVip myVip = new MyVip();
//			myVip.setCreateTime(new Date());
//			myVip.setExpireTime(DateUtil.endOfYear(new Date()));
//			myVip.setPrice(BigDecimal.ZERO);
//			myVip.setName(vipDictionary.getName());
//			myVip.setUserId(record.getUserId());
//			myVip.setVipType(record.getUserId());
//			myVipMapper.insert(myVip);
//		}
		return 1;
	}

	public int updateByPrimaryKeySelective(User record) {
		return userMapper.updateByPrimaryKeySelective(record);
	}

	public User selectByPrimaryKey(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	public int addReferees(Integer userId,Integer referees){
		return userMapper.addReferees(userId,referees);
	}

	/**
	 * 查询团队列表
	 *
	 * @param userId
	 * @param type
	 * @param search
	 * @param pageBean
	 * @return
	 */
	public PageResult selectListByType(Integer userId, Integer type, String search, PageBean pageBean) {
		PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
		Page<UserTeamVO> userTeamVOS  = userMapper.selectListByType(userId,type,search);
		return new PageResult(userTeamVOS.getTotal(),userTeamVOS.getResult());
	}

	/**
	 * 我的申请列表 只有区域经理有
	 *
	 * @param userId
	 * @param search
	 * @param type
	 * @param pageBean
	 * @return
	 */
	public PageResult selectApplyList(Integer userId, String search, Integer type, PageBean pageBean) {
		PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
		Page<UserTeamApplyVO> userTeamVOS  = userMapper.selectByApplyList(userId,type, search);
		return new PageResult(userTeamVOS.getTotal(),userTeamVOS.getResult());
	}

//	/**
//	 * 定时 把七天内没有填写推荐人的人填写成公司的领导
//	 */
//	public void updateUserDefaultParent() {
//		//查询注册7天内没有绑定推荐人的人
//		List<User> userList = userMapper.selectSevenNoBindUserList();
//
//		for (User user : userList) {
//			user.setRefereesUserId(22);
//			user.setHasRealParent(0);
//
//		}
//	}

	public User selectByCode(String recommendCode){
		return userMapper.selectByCode(recommendCode);
	}
}
